package com.oceansoft.core.common.util.impexp4xls;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;

import com.oceansoft.core.common.util.StringUtil;
import com.oceansoft.core.common.util.impexp4xls.help.BuildRecordSQLHelper;
import com.oceansoft.core.common.util.impexp4xls.help.BuildXLSHelper;
import com.oceansoft.core.common.util.impexp4xls.help.ConfigFileLoadHelper;

public class POIExporterImpl implements XLSExporter {

	// 全局配置
	HashMap<String, Object> configure = new HashMap<String, Object>();
	Workbook hssf;

	@SuppressWarnings("rawtypes")
	public void loadConfig(String configName, Map extraCfg) {
		String configPath = ConfigFileLoadHelper.getConfigPath(configName);
		ConfigFileLoadHelper.loadCofingFile(configPath, configure, extraCfg);
	}
	
	public void loadConfig4Custom(String configName, Map extraCfg) {
		String configPath = ConfigFileLoadHelper.getConfigPath(configName);
		ConfigFileLoadHelper.loadCofingFile4Custom(configPath, configure, extraCfg);
	}

	public void setConfig(Map<String, Object> configMap) {
		this.configure = (HashMap<String, Object>) configMap;
	}

	public void write(OutputStream out) {
		try {
			hssf.write(out);

		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("--", e);
		}
	}

	@SuppressWarnings("unchecked")
	public void buildXLS() {
		Map<String, String> entityProp = (Map<String, String>) configure.get("customProperty");
		String tableName = entityProp.get("tableName");
		List<String> fields = (List<String>) configure.get("fieldNameList");
		String sql = BuildRecordSQLHelper.buildRecordSQL(tableName, fields);
		bluidXLS(sql);
	}

	public void bluidXLS(String sql) {
		
	}

	public void buildXLS(List<?> record) {
		hssf = BuildXLSHelper.buildXLS(configure, record);
	}

	@SuppressWarnings("rawtypes")
	public void buildXLS(List<?> record, Map customMap) {
		hssf = BuildXLSHelper.buildXLS(configure, record, customMap);
	}

	public void exportXLS(OutputStream out, String configfile, List<?> record, String sql) {
		exportXLS(out, configfile, record, sql, null);
	}

	@SuppressWarnings("rawtypes")
	public void exportXLS(OutputStream out, String configfile, List<?> record, String sql, Map extraCfg) {
		loadConfig(configfile, extraCfg);
		if (record == null || record.isEmpty()) {
			if (StringUtil.isNotNull(sql)) {
				bluidXLS(sql);
			} else {
				buildXLS();
			}
		} else {
			buildXLS(record);
		}
		write(out);
	}

	@SuppressWarnings("rawtypes")
	public void exportXLS(OutputStream out, String configfile, List<?> record, Map customMap) {
		loadConfig(configfile, null);
		buildXLS(record, customMap);
		write(out);
	}

	@SuppressWarnings("rawtypes")
	public void exportXLSCustom(OutputStream out, String configfile, Map record, Map customMap) {
		//读取配置文件也要定制
		loadConfig4Custom(configfile, null);
		hssf = BuildXLSHelper.buildXLSCustom(configure, record, customMap);
		write(out);
	}
}
